- name: upgrade koschei
  hosts: koschei-stg;koschei
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - include: "{{ handlers }}/restart_services.yml"
  roles:
  - koschei

  pre_tasks:
  - name: schedule nagios downtime
    nagios: action=downtime minutes=20 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true
    when: env != 'staging'
  - name: stop services
    service: name="{{ item }}" state=stopped
    with_items:
    - koschei-scheduler
    - koschei-polling
    - koschei-resolver
    - koschei-watcher
    - httpd
  - name: clean yum metadata
    command: yum clean all {% if env == 'staging' %} --enablerepo="koschei-jenkins" {% endif %}
  - name: upgrade koschei package from jenkins
    yum: name="koschei" state=latest enablerepo="koschei-jenkins"
    when: env == 'staging'
  - name: upgrade koschei from main repo
    yum: name="koschei" state=latest
    when: env != 'staging'
  - name: run db migration
    become: yes
    become_user: koschei
    command: alembic -c /usr/share/koschei/alembic.ini upgrade head

  post_tasks:
  - name: start httpd
    service: name="httpd" state=started
